package com.lf.service.impl;

import com.lf.entity.ResponseWrapper;
import com.lf.mapper.MapperList;
import com.lf.mapper.SomeValMapper;
import com.lf.service.OcrService;
import com.lf.util.FtpClientUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.net.ftp.FTPClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @aouhor YangHuiLiang
 * @create 2019/6/27
 */
@Slf4j
@Service
public class OcrServiceImpl extends MapperList implements OcrService {

    @Transactional(propagation= Propagation.REQUIRED)
    @Override
    public String fileUpload(MultipartFile multipartFile) throws IOException {

//      new一个FtpClient对象
        FtpClientUtils ftpClientUtils = new FtpClientUtils();

//      添加 ip、端口、用户名、密码
        FTPClient ftp = ftpClientUtils.getConnectionFTP();

//      调用上传接口 传入ftp对象、ftp存放的位置、文件名、文件流
        String fileName = ftpClientUtils.uploadFile(ftp, multipartFile.getOriginalFilename(), multipartFile.getInputStream());

//      断开ftp服务器
        ftpClientUtils.closeFTP(ftp);
        return fileName;
    }


    @Transactional(propagation=Propagation.REQUIRED)
    @Override
    public boolean deleteFile(String fileName) {
//      new一个FtpClient对象
        FtpClientUtils ftpClientUtils = new FtpClientUtils();

//      添加 ip、端口、用户名、密码
        FTPClient ftp = ftpClientUtils.getConnectionFTP();

        return ftpClientUtils.deleteFile(ftp,fileName);
    }

    @Transactional(propagation=Propagation.REQUIRED)
    @Override
    public ResponseWrapper updateText() throws InterruptedException {
        try {
            for (int i = 0;i<10;i++) {
                Thread.sleep(500);
                log.info(Thread.currentThread().getId() + " 获取到写锁了");
            }
        } catch (Exception e) {
            throw e;
        }
        return ResponseWrapper.succeed();
    }


    @Transactional(propagation=Propagation.REQUIRED)
    @Override
    public ResponseWrapper getText() throws InterruptedException{
        try {
            for (int i = 0;i<10;i++) {
                Thread.sleep(500);
                log.info(Thread.currentThread().getId() + "获取到读锁了");
            }
        } catch (Exception e) {
            throw e;
        }
        return ResponseWrapper.succeed();
    }

}